package com.shouhe.mysap.controller.pur;


import com.baomidou.mybatisplus.extension.service.IService;
import com.shouhe.mysap.common.MySapResult;
import com.shouhe.mysap.common.PageResult;
import com.shouhe.mysap.common.status.PlanStatus;
import com.shouhe.mysap.common.status.PurStatus;
import com.shouhe.mysap.entity.PlanNoSupplier;
import com.shouhe.mysap.entity.PurAskPrice;
import com.shouhe.mysap.entity.PurPerson;
import com.shouhe.mysap.service.PlanNoSupplierService;
import com.shouhe.mysap.service.PurAskPriceService;
import com.shouhe.mysap.service.PurPersonService;
import com.shouhe.mysap.util.PageResultUtil;
import com.shouhe.mysap.util.SelectUtil;
import com.shouhe.mysap.util.UpdateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 守鹤
 * @since 2020-06-08
 */
@RestController
@RequestMapping("/pur-ask-price")
public class PurAskPriceController {


    @Autowired
    private PurAskPriceService purAskPriceService;

    @Autowired
    private PlanNoSupplierService planNoSupplierService;

    /**
     * @描述 得到全部的采购人员
     * @参数
     * @返回值
     */
    @RequestMapping("/getPurAskPrices")
    public PageResult getPurPersons(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows) {
        PageResult pageResult = PageResultUtil.getPageResult(page, rows, purAskPriceService);
        return pageResult;
    }


    @RequestMapping("/purConfirmPurGroup")
    public MySapResult purConfirmPurGroup(@RequestParam("ids[]") Integer[] ids){
        //修改采购询价报表中记录的状态
        List<PurAskPrice> list = new ArrayList<>();
        for (Integer id : ids) {
            PurAskPrice price = new PurAskPrice();
            price.setId(id);
            price.setStatus(PurStatus.PUR_ASK_PRICE_HAS_PURGROUP);
            list.add(price);
        }
        MySapResult result = UpdateUtil.updateObjectListById("采购秘书确认采购组", list, purAskPriceService);
        if(!result.isSuccess()){
            return  result;
        }

        //修改供应链询价报表中记录的状态
        //得到物料与采购组的对应关系
        List<PurAskPrice> purAskPrices = SelectUtil.selectListIn("id", Arrays.asList(ids), purAskPriceService);
        HashMap<String, Integer> map = new HashMap<>();
        List<String> codes = new ArrayList<>();
        for (PurAskPrice purAskPrice : purAskPrices) {
            map.put(purAskPrice.getThingCode(),purAskPrice.getPurGroup());
            codes.add(purAskPrice.getThingCode());
        }

        List<PlanNoSupplier> noSuppliers = SelectUtil.selectListIn("thing_code", codes, planNoSupplierService);
        for (PlanNoSupplier noSupplier : noSuppliers) {
            noSupplier.setStatus(PlanStatus.PLAN_NO_SUPPLIER_HAS_PURGROUP);
            noSupplier.setPurGroup(map.get(noSupplier.getThingCode()));
        }
        MySapResult result1 = UpdateUtil.updateObjectListById("供应链询价报表状态更新为确认了采购组", noSuppliers, planNoSupplierService);
        return result1;
    }

    /**
     * @描述 通过id删除 采购员
     * @参数
     * @返回值
     */
    @RequestMapping("/updatePurAskPrice")
    public MySapResult updatePurAskPrice(PurAskPrice purAskPrice) {
        return UpdateUtil.updateObjectById("询价报表中更新采购组",purAskPrice,purAskPriceService);
    }


}

